﻿<%@ Page Language="C#" AutoEventWireup="true"  Inherits="ZX.Web.UI.Controls.BasePage" %>
//-----------------------------------------------------------
// 表单自定义的数据
// Version :V 0.1.0
// ----------------------------------------------------------
_DataTable<%=EntityName %> = {
    verion: "v 0.1.0",
    tableId: "",
    author: "江怀玉",
    Editable: false,
    FExpression: "5=2",
    Entity: "<%=EntityName %>",
    ///列名集合
    Column: [],
    ///行值集合
    Rows: [],
    ///定义当前行
    CureentRow: "",
    SelectedIndex: -1,
    ///获取行
    getRow: function (i) {
        // this.SelectedIndex = i;
        return this.Rows[i];
    },
    FirstHTML: "",

    _DataTableURL: "AjaxData/DataTableJsonlist.aspx"
    ,
    _BusinessID: ""
    ,
    ///获取当前实体
    getEntity: function () {
        return this.Entity;
        //return $($("#<%=EntityName %>Details table")[0]).attr("name");
    },
    ///获取列名集合
    getColumn: function () {
        var _Column = [];
        $($("#<%=EntityName %>Details table tbody")[0]).find("input,select").each(function () {
            _Column.push($(this).attr("name"));
            $(this).val("");
        });
        this.Column = _Column;
    },
    ///获取当前行指定的列值
    getColumnValue: function (o) {
        if (typeof (o) == "String") {
            return eval("this.CureentRow." + o);
        }
        else {
            return eval("this.CureentRow." + Column[o]);
        }
    },
    setDataTableURL: function (xurl) {
        this._DataTableURL = xurl;
    },
    setBusinessID: function (xid) {
        if (typeof (xid) == "undefined") { this._BusinessID = ""; }
        else { this._BusinessID = xid; }
       
    },
    ///数据
    _DataBind: function () {
        //alert(this.Entity);
        this.Entity = this.getEntity();
        this.getColumn();
        ///this.DataBind(this.Entity);
        this.getDataSource();
    },
    ///获取数据源
    getDataSource: function () {
        //debugger
        //alert(this.FExpression);
        var strUrl = "";
        if (this.FExpression == "" || this.FExpression.split('=')[1] == "''") {
            strUrl = this._DataTableURL + "?r=" + Math.random() + "&order=RecID&pageindex=1&pagesize=5000&entity=" + this.Entity;
        } else {
            strUrl = this._DataTableURL + "?r=" + Math.random() + "&order=RecID&condition=" + this.FExpression + "&pageindex=1&pagesize=5000&entity=" + this.Entity;
        }
        this.getColumn();
        var col = this.Column.length;

        if (cust_DataBind<%=EntityName %>) {
            cust_DataBind<%=EntityName %>($("#" + this.Entity + " tr"));
        }
        this.FirstHTML = $("#" + this.Entity + " tr")[1].outerHTML;
       
        $.get(strUrl, function (data, textStatus) {
            debugger
            //alert(data);
            if (data != "{\"total\":0,\"rows\":}" && data != "[{\"N\":\"null\"}]") {
                _DataTable<%=EntityName %>.Rows = eval("(" + data + ")").rows;
            } else {
                // alert(_DataTable<%=EntityName %>.Entity
                //var o = clone(eval(_DataTable<%=EntityName %>.Entity));
                //_DataTable<%=EntityName %>.Rows.push(o);
            }
            _DataTable<%=EntityName %>.Render();
            var trs = $("#" + _DataTable<%=EntityName %>.Entity + " tr");
            for (var i = 0; i < _DataTable<%=EntityName %>.Rows.length; i++) {
                $(trs[i + 1]).JSONToForm(_DataTable<%=EntityName %>.Rows[i]);
            }
            var th = $("<thead><thead>").append($("#" + _DataTable<%=EntityName %>.Entity + " tr:first"));
            $("#" + _DataTable<%=EntityName %>.Entity).prepend(
                th
            );
            $("#" + _DataTable<%=EntityName %>.Entity + " tr:first").remove();
            if (typeof (parent._CancelFlag) != "undefined") {
                //退回标标/ID/草稿
                if (parent._CancelFlag == "4" || _DataTable<%=EntityName %>._BusinessID == "" || getPar1("statues") == "2") {
                    {
                        $("#" + _DataTable<%=EntityName %>.Entity + " thead:first").prepend(
                            $("<tr><td colspan=\"" + col + "\"><input id='_btnNew' onclick='_DataTable<%=EntityName %>.AddRow();return false;' type='button'' value='新增'><input type='button' id='_btnNew' onclick='_DataTable<%=EntityName %>.DeleteRow();return false;' value='删除'/></td></tr>")
                             );
                    }
                } else {
                    //明细表权限。
                    if (typeof (DetailsFormRoles) != "undefined") {
                        if (DetailsFormRoles != null) {
                            if (typeof (DetailsFormRoles.EditorPropertyValue) != "undefined") {
                                if (DetailsFormRoles.EditorPropertyValue.length > 0) {
                                    $("#" + _DataTable<%=EntityName %>.Entity + " thead:first").prepend(
                                    $("<tr><td colspan=\"" + col + "\"><input id='_btnNew' onclick='_DataTable<%=EntityName %>.AddRow();return false;' type='button'' value='新增'><input type='button' id='_btnNew' onclick='_DataTable<%=EntityName %>.DeleteRow();return false;' value='删除'/></td></tr>")
                                     );
                                }
                            }

                        }
                    }

                }
            }
            else {
                //退回标标/ID/草稿
               
                if (_DataTable<%=EntityName %>._BusinessID == "") {
                    $("#" + _DataTable<%=EntityName %>.Entity + " thead:first").prepend(
                    $("<tr><td colspan=\"" + col + "\"><input id='_btnNew' onclick='_DataTable<%=EntityName %>.AddRow();return false;' type='button'' value='新增'><input type='button' id='_btnNew' onclick='_DataTable<%=EntityName %>.DeleteRow();return false;' value='删除'/></td></tr>")
                );
                } else {
                    //非流程
                    //alert(getPar1("entity").indexOf("bpm_"));
                    if (getPar1("entity").indexOf("bpm_") == -1 || getPar1("statues") == "2") {
                        $("#" + _DataTable<%=EntityName %>.Entity + " thead:first").prepend(
                    $("<tr><td colspan=\"" + col + "\"><input id='_btnNew' onclick='_DataTable<%=EntityName %>.AddRow();return false;' type='button'' value='新增'><input type='button' id='_btnNew' onclick='_DataTable<%=EntityName %>.DeleteRow();return false;' value='删除'/></td></tr>")
                    );

                    }
                }
            }

            if (data != "{\"total\":0,\"rows\":}") {
                fnSelected<%=EntityName %>();
            }
            //$("#" + _DataTable<%=EntityName %>.Entity + " tr:first").remove();
        });
    },
    ///重写控制
    Render: function () {
        //var trs = $($($("#" + this.Entity + " tr")[1]).html());
        $($("#" + this.Entity + " tr")[1]).remove();

        if (this.Rows.length > 0) {
            for (var i = 0; i < this.Rows.length; i++) {
                var trs = $(this.FirstHTML.replace("data_row", "id" + i));
                trs.appendTo($($("#<%=EntityName %>Details table")));
                //debugger
                if (typeof (parent._CancelFlag) != "undefined") {
                    //退回标标/ID/草稿
                    if (parent._CancelFlag != "4" && _DataTable<%=EntityName %>._BusinessID != "" && getPar1("statues") != "2") {

                        if (typeof (DetailsFormRoles) != "undefined") {
                            if (DetailsFormRoles != null) {
                                if (typeof (DetailsFormRoles.EditorPropertyValue) != "undefined") {
                                    if (DetailsFormRoles.EditorPropertyValue.length > 0) {
                                        trs.setInputReadOnly(false);
                                    } else { trs.setInputReadOnly(true); }
                                } else { trs.setInputReadOnly(true); }
                            } else { trs.setInputReadOnly(true); }
                        } else { trs.setInputReadOnly(true); }
                    }
                }
                else {
                    if (_DataTable<%=EntityName %>._BusinessID != "") {
                        if (getPar1("entity").indexOf("bpm_") > -1) {
                            trs.setInputReadOnly(true);
                        }
                    }
                }
            }
            //this.AddRow();
        }
        //初始化
        if (typeof (initControls) == "function") {
            var _time = $("input[type=Time]");
            initControls(_time, "Time");
            var _date = $("input[type=Date]");
            initControls(_date, "Date");
        }
    },
    //增加行
    AddRow: function () {
        //debugger 
        var o = clone(eval(_DataTable<%=EntityName %>.Entity));
        this.Rows.push(o);
        $("#<%=EntityName %>Details tbody tr").unbind();
        var i = this.Rows.length - 1
        var trs = $(this.FirstHTML.replace("data_row", "id" + this.Rows.length - 1));
        trs.appendTo($($("#<%=EntityName %>Details table")));
        trs.find("input").bind('blur', function () {
            _DataTable<%=EntityName %>.Modify();
        });
        $(trs).JSONToForm(_DataTable<%=EntityName %>.Rows[i]);
        fnSelected<%=EntityName %>();
        if (typeof (fnCustomeRowAddEvent) == "function") {
            fnCustomeRowAddEvent();
        }
        //初始化
        if (typeof (initControls) == "function") {
            var _time = $("input[type=Time]");
            initControls(_time, "Time");
            var _date = $("input[type=Date]");
            initControls(_date, "Date");

        }
    },
    //删除行
    DeleteRow: function () {
        //debugger
        var i = 0;
        //$("#<%=EntityName %>Details tbody tr")
        $("#<%=EntityName %>Details tbody .selected").remove();
        this.Rows.REMOVE(this.CureentRow);
    },
    //修改
    Modify: function () {
        $("#<%=EntityName %>Details tbody tr.selected").SetFormValueToObject(this.CureentRow);
    },
    //选定某条数据
    Selected: function (e) {
        this.SelectedIndex = 0;
        var trs1 = $("#<%=EntityName %>Details tbody tr");
        trs1.each(function (em) {
            var _class = $(trs1[em]).attr("class");
            if (_class == undefined) {
                return;
            }
            if ($(trs1[em]).attr("class").indexOf("selected") > -1) {
                _DataTable<%=EntityName %>.SelectedIndex = em;
            }
        });
        if (_DataTable<%=EntityName %>.SelectedIndex.toString() == "NaN") {
            _DataTable<%=EntityName %>.SelectedIndex = 0;
        }
        this.CureentRow = this.Rows[this.SelectedIndex];
        //$("#test")[0].innerHTML = (this.CureentRow.ID);
    },
    ///在数据提交前将表格中数据跟数据对象同步
    GetData: function () {
        var trs = $("#<%=EntityName %>Details tbody tr");
        trs.each(function (em) {
            $("#<%=EntityName %>Details tbody tr").removeClass("selected");
            $(trs[em]).addClass("selected");
            _DataTable<%=EntityName %>.Selected($(trs[em]));
            _DataTable<%=EntityName %>.Modify();
        });

    }
}
 _DataTable<%=EntityName %>.FExpression= fExpression ;
//选择
var fnSelected<%=EntityName %> = function () {
    $("#<%=EntityName %>Details tbody tr").bind('click', function (evt) {
        _DataTable<%=EntityName %>.Modify();
        $("#<%=EntityName %>Details tr").removeClass("selected");
        $(this).addClass("selected");
        _DataTable<%=EntityName %>.Selected($(this));
    });
}

/// 检查数据
var fnCheckEmpty<%=EntityName %> = function () {
    var trs = $("#<%=EntityName %>Details tbody tr");
    if (trs.length <= 1) {
        return true;
    }
    trs.each(function (em) {
        var t = $(trs[em]).find("input");
        var _t = "";
        t.each(function (_em) {
            _t = _t + $(t[_em]).val();
        });
        if (_t == "") {
            return false;
        }
    });
    return true;
}

function cust_DataBind<%=EntityName %>(o) {
    var selective = o.find("select");
    selective.each(function (s) {
        //debugger
        try {
            var o = eval("ds_Detail_" + selective[s].name);
            if (typeof (o) != "undefined") {
                $(selective).comBind(o, "ValueMember", "DisplayMember");
            }
        } catch (err) {
        }
    });
}
_DataTable<%=EntityName %>.setBusinessID(getPar1("id"));//
_DataTable<%=EntityName %>._DataBind();
setTimeout(fnSelected<%=EntityName %>,500);